package fr.afcepf.ai89.esharing.metier;

import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import fr.afcepf.ai89.esharing.data.IDAOAvis;
import fr.afcepf.ai89.esharing.data.IDAOEmprunt;
import fr.afcepf.ai89.esharing.data.IDAOSousAvis;
import fr.afcepf.ai89.esharing.entity.Avis;
import fr.afcepf.ai89.esharing.entity.Emprunt;
import fr.afcepf.ai89.esharing.entity.Proprietaire;
import fr.afcepf.ai89.esharing.entity.SousAvis;

public class BusinessPretAem implements IBusinessPretAem {

	@Override
	public Emprunt getPret(int idPret) {
		Emprunt emprunt= new Emprunt();
		try {
			
			Context ctx;
			ctx = new InitialContext();
			IDAOEmprunt proxyDataEmprunt = null;
			
			
			try {
				
				proxyDataEmprunt = (IDAOEmprunt)ctx.lookup("java:global/ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			catch (Exception e ) {
				
				proxyDataEmprunt = (IDAOEmprunt)ctx.lookup("ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			
			//msg = proxyDataMsg.getMsgP.rive(idMsgPrive);
			emprunt =proxyDataEmprunt.getEmprunt(idPret);
				
		}
		catch (NamingException e) {
			
			e.printStackTrace();
			
		}
		
		return emprunt;
	}

	@Override
	public int addAvisSurEmprunteur(Emprunt emprunt) {

		int id = 0;
		
		try {
			
			Context ctx;
			ctx = new InitialContext();
			IDAOAvis proxyDataAvis = null;
			IDAOSousAvis proxyDataSousAvis = null;
			IDAOEmprunt proxyDataEmprunt = null;
			
			try {
				
				proxyDataAvis = (IDAOAvis)ctx.lookup("java:global/ESharingPhase1/DAOAvis!fr.afcepf.ai89.esharing.data.IDAOAvis");
			
			}
			catch (Exception e ) {
				
				proxyDataAvis = (IDAOAvis)ctx.lookup("ESharingPhase1/DAOAvis!fr.afcepf.ai89.esharing.data.IDAOAvis");
			
			}
			
			try {
				
				proxyDataSousAvis = (IDAOSousAvis)ctx.lookup("java:global/ESharingPhase1/DAOSousAvis!fr.afcepf.ai89.esharing.data.IDAOSousAvis");
			
			}
			catch (Exception e ) {
				
				proxyDataSousAvis = (IDAOSousAvis)ctx.lookup("ESharingPhase1/DAOSousAvis!fr.afcepf.ai89.esharing.data.IDAOSousAvis");
			
			}
			
			try {
				
				proxyDataEmprunt = (IDAOEmprunt)ctx.lookup("java:global/ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			catch (Exception e ) {
				
				proxyDataEmprunt = (IDAOEmprunt)ctx.lookup("ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			
			int idAv = proxyDataAvis.addAvis(emprunt.getAvisSurEmprunteur());
			Avis nouveauAvis = proxyDataAvis.getAvis(idAv);
			nouveauAvis.setSousAvis(emprunt.getAvisSurEmprunteur().getSousAvis());
			
			List<SousAvis> lesSA = new ArrayList<SousAvis>();
			
			for (SousAvis sa : nouveauAvis.getSousAvis()) {
				
				sa.setAvis(nouveauAvis);
				int idSA = proxyDataSousAvis.addSousAvis(sa);
				lesSA.add(proxyDataSousAvis.getSousAvis(idSA));
				
			}
			
			nouveauAvis.setSousAvis(lesSA);
			emprunt.setAvisSurEmprunteur(nouveauAvis);
			
			id = proxyDataEmprunt.laisserAvisSurEmprunteur(emprunt);
				
		}
		catch (NamingException e) {
			
			e.printStackTrace();
			
		}
		
		return id;

				
	}

	@Override
	public int validerEmprunt(Emprunt emprunt) {
		int i=0;
try {
			
			Context ctx;
			ctx = new InitialContext();
			IDAOEmprunt proxyDataPret = null;
			
			
			try {
				
				proxyDataPret = (IDAOEmprunt)ctx.lookup("java:global/ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			catch (Exception e ) {
				
				proxyDataPret = (IDAOEmprunt)ctx.lookup("ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			
			//msg = proxyDataMsg.getMsgP.rive(idMsgPrive);
			   i=proxyDataPret.validerEmprunt(emprunt);
				
		}
		catch (NamingException e) {
			
			e.printStackTrace();
			
		}
		
		return i;
	}

	@Override
	public int refuserEmprunt(Emprunt emprunt) {
		int i=0;
		try {
					
					Context ctx;
					ctx = new InitialContext();
					IDAOEmprunt proxyDataPret = null;
					
					
					try {
						
						proxyDataPret = (IDAOEmprunt)ctx.lookup("java:global/ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
					
					}
					catch (Exception e ) {
						
						proxyDataPret = (IDAOEmprunt)ctx.lookup("ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
					
					}
					
					//msg = proxyDataMsg.getMsgP.rive(idMsgPrive);
					   i=proxyDataPret.refuserEmprunt(emprunt);
						
				}
				catch (NamingException e) {
					
					e.printStackTrace();
					
				}
				
				return i;
	}

	@Override
	public int majEmprunt(Emprunt emprunt) {
		int i=0;
		try {
					
					Context ctx;
					ctx = new InitialContext();
					IDAOEmprunt proxyDataPret = null;
					
					
					try {
						
						proxyDataPret = (IDAOEmprunt)ctx.lookup("java:global/ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
					
					}
					catch (Exception e ) {
						
						proxyDataPret = (IDAOEmprunt)ctx.lookup("ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
					
					}
					
					//msg = proxyDataMsg.getMsgP.rive(idMsgPrive);
					   i=proxyDataPret.majEmprunt(emprunt);
						
				}
				catch (NamingException e) {
					
					e.printStackTrace();
					
				}
				
				return i;
	}

	@Override
	public int annulerEmprunt(Emprunt emprunt) {
		int i=0;
		try {
					
					Context ctx;
					ctx = new InitialContext();
					IDAOEmprunt proxyDataPret = null;
					
					
					try {
						
						proxyDataPret = (IDAOEmprunt)ctx.lookup("java:global/ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
					
					}
					catch (Exception e ) {
						
						proxyDataPret = (IDAOEmprunt)ctx.lookup("ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
					
					}
					
					//msg = proxyDataMsg.getMsgP.rive(idMsgPrive);
					   i=proxyDataPret.annulerEmprunt(emprunt);
						
				}
				catch (NamingException e) {
					
					e.printStackTrace();
					
				}
				
				return i;
	}

	@Override
	public int confirmerAemRendu(Emprunt emprunt) {
		int i=0;
		try {
					
					Context ctx;
					ctx = new InitialContext();
					IDAOEmprunt proxyDataPret = null;
					
					
					try {
						
						proxyDataPret = (IDAOEmprunt)ctx.lookup("java:global/ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
					
					}
					catch (Exception e ) {
						
						proxyDataPret = (IDAOEmprunt)ctx.lookup("ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
					
					}
					
					//msg = proxyDataMsg.getMsgP.rive(idMsgPrive);
					   i=proxyDataPret.marquerAemRendu(emprunt);
						
				}
				catch (NamingException e) {
					
					e.printStackTrace();
					
				}
				
				return i;
	}

	@Override
	public List<Emprunt> getPretsPasses(Proprietaire prop) {
		List<Emprunt> pretsPasses=new ArrayList<>();
		try {
			
			Context ctx;
			ctx = new InitialContext();
			IDAOEmprunt proxyDataPret = null;
			
			
			try {
				
				proxyDataPret = (IDAOEmprunt)ctx.lookup("java:global/ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			catch (Exception e ) {
				
				proxyDataPret = (IDAOEmprunt)ctx.lookup("ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			
			//msg = proxyDataMsg.getMsgP.rive(idMsgPrive);
			  pretsPasses =proxyDataPret.getPretsPasses(prop);
				
		}
		catch (NamingException e) {
			
			e.printStackTrace();
			
		}
		
		return pretsPasses;
	}

	@Override
	public List<Emprunt> getPretsEnCours(Proprietaire prop) {
		List<Emprunt> pretsEnCours=new ArrayList<>();
		try {
			
			Context ctx;
			ctx = new InitialContext();
			IDAOEmprunt proxyDataPret = null;
			
			
			try {
				
				proxyDataPret = (IDAOEmprunt)ctx.lookup("java:global/ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			catch (Exception e ) {
				
				proxyDataPret = (IDAOEmprunt)ctx.lookup("ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			
			//msg = proxyDataMsg.getMsgP.rive(idMsgPrive);
			pretsEnCours =proxyDataPret.getPretsEnCours(prop);
				
		}
		catch (NamingException e) {
			
			e.printStackTrace();
			
		}
		
		return pretsEnCours;
	}

	@Override
	public List<Emprunt> getPretsPlannifies(Proprietaire prop) {
		List<Emprunt> pretsPlannifies=new ArrayList<>();
		try {
			
			Context ctx;
			ctx = new InitialContext();
			IDAOEmprunt proxyDataPret = null;
			
			
			try {
				
				proxyDataPret = (IDAOEmprunt)ctx.lookup("java:global/ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			catch (Exception e ) {
				
				proxyDataPret = (IDAOEmprunt)ctx.lookup("ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			
			//msg = proxyDataMsg.getMsgP.rive(idMsgPrive);
			pretsPlannifies =proxyDataPret.getPretsPlannifies(prop);
				
		}
		catch (NamingException e) {
			
			e.printStackTrace();
			
		}
		
		return pretsPlannifies;
	}

	@Override
	public List<Emprunt> getPretsNonValides(Proprietaire prop) {
		List<Emprunt> pretsNonValides=new ArrayList<>();
		try {
			
			Context ctx;
			ctx = new InitialContext();
			IDAOEmprunt proxyDataPret = null;
			
			
			try {
				
				proxyDataPret = (IDAOEmprunt)ctx.lookup("java:global/ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			catch (Exception e ) {
				
				proxyDataPret = (IDAOEmprunt)ctx.lookup("ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			
			//msg = proxyDataMsg.getMsgP.rive(idMsgPrive);
			pretsNonValides =proxyDataPret.getPretsNonValides(prop);
				
		}
		catch (NamingException e) {
			
			e.printStackTrace();
			
		}
		
		return pretsNonValides;
	}

	@Override
	public List<Emprunt> getPretsAnnules(Proprietaire prop) {
		List<Emprunt> pretsAnnules=new ArrayList<>();
		try {
			
			Context ctx;
			ctx = new InitialContext();
			IDAOEmprunt proxyDataPret = null;
			
			
			try {
				
				proxyDataPret = (IDAOEmprunt)ctx.lookup("java:global/ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			catch (Exception e ) {
				
				proxyDataPret = (IDAOEmprunt)ctx.lookup("ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			
			//msg = proxyDataMsg.getMsgP.rive(idMsgPrive);
			pretsAnnules =proxyDataPret.getPretsAnnules(prop);
				
		}
		catch (NamingException e) {
			
			e.printStackTrace();
			
		}
		
		return pretsAnnules;
	}

	@Override
	public List<Emprunt> getPretsRefuses(Proprietaire prop) {
		List<Emprunt> pretsRefuses=new ArrayList<>();
		try {
			
			Context ctx;
			ctx = new InitialContext();
			IDAOEmprunt proxyDataPret = null;
			
			
			try {
				
				proxyDataPret = (IDAOEmprunt)ctx.lookup("java:global/ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			catch (Exception e ) {
				
				proxyDataPret = (IDAOEmprunt)ctx.lookup("ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			
			//msg = proxyDataMsg.getMsgP.rive(idMsgPrive);
			pretsRefuses =proxyDataPret.getPrets(prop);
				
		}
		catch (NamingException e) {
			
			e.printStackTrace();
			
		}
		
		return pretsRefuses;
	}

	@Override
	public List<Emprunt> getPrets(Proprietaire prop) {
		List<Emprunt> prets=new ArrayList<>();
		try {
			
			Context ctx;
			ctx = new InitialContext();
			IDAOEmprunt proxyDataPret = null;
			
			
			try {
				
				proxyDataPret = (IDAOEmprunt)ctx.lookup("java:global/ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			catch (Exception e ) {
				
				proxyDataPret = (IDAOEmprunt)ctx.lookup("ESharingPhase1/DAOEmprunt!fr.afcepf.ai89.esharing.data.IDAOEmprunt");
			
			}
			
			//msg = proxyDataMsg.getMsgP.rive(idMsgPrive);
			prets =proxyDataPret.getPrets(prop);
				
		}
		catch (NamingException e) {
			
			e.printStackTrace();
			
		}
		
		return prets;
	}

}
